package dms.presentation.action.teacher;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.actions.DispatchAction;

import dms.dto.StudentDTO;
import dms.model.Building;
import dms.model.Classes;
import dms.model.Dormitory;
import dms.model.Faculty;
import dms.model.School;
import dms.model.role.Student;
import dms.model.role.wrapper.StudentAffairTeacherWrapper;
import dms.model.util.PageIterator;
import dms.service.AuthenticationService;
import dms.service.BuildingService;
import dms.service.DormitoryService;
import dms.service.SchoolService;
import dms.service.StudentQueryService;
import dms.util.Constant;
import dms.util.ContextUtil;

public class StudentAffairAction extends DispatchAction{
	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
		StudentAffairTeacherWrapper wrapper = (StudentAffairTeacherWrapper) ContextUtil.getContext().getBean("studentAffairTeacherWrapper");
		StudentQueryService studentQueryService = (StudentQueryService) ContextUtil.getContext().getBean("studentQueryService");
		School school=wrapper.getStudentAffairTeacher().getSchool();
		int start;
		int count;
		try{
			start=Integer.valueOf(request.getParameter("start"));
			count=Integer.valueOf(request.getParameter("count"));
		}
		catch(Exception e)
		{
			start = Constant.START;
			count = Constant.COUNT;
		}
		PageIterator <Student>students=studentQueryService.findBySchool(school, start, count);
		request.setAttribute("student", students);
		return mapping.findForward("list");
}
	public ActionForward addForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		StudentDTO studentDTO=(StudentDTO)form;
		ActionErrors errors=studentDTO.validate(mapping, request);
		int page;
		try{
			page=Integer.valueOf(request.getParameter("page"));
		}
		catch(Exception ex)
		{
			page=1;
		}
		if (errors != null && errors.isEmpty()) {
            if (page== 1)
            {
            	SchoolService schoolService=(SchoolService) ContextUtil.getContext().getBean("schoolService");
            	StudentAffairTeacherWrapper wrapper = (StudentAffairTeacherWrapper) ContextUtil.getContext().getBean("studentAffairTeacherWrapper");
            	School school=wrapper.getStudentAffairTeacher().getSchool();
            	PageIterator<Faculty> faculty=schoolService.findFacultyBySchool(school, 0, 0);
            	request.setAttribute("faculty", faculty);
            	request.setAttribute("school", school);
            	return mapping.findForward("input1");
            }
           if (page == 2)
           {
        	   
        	   SchoolService schoolService=(SchoolService) ContextUtil.getContext().getBean("schoolService");
        	   BuildingService buildingService=(BuildingService)ContextUtil.getContext().getBean("buildingService");
        	   PageIterator<Building> building=buildingService.findAll(0, 0);
        	   Faculty faculty=null;
        	   Long facultyId=null;
        	   try{
        		   facultyId=Long.valueOf(request.getParameter("faculty"));
        		   faculty=schoolService.findFacultyById(facultyId);
        	   }
        	   catch (Exception e) {
        		   e.printStackTrace();
        	   }
        	   PageIterator<Classes> classes=schoolService.findClassesByFaculty(faculty, 0, 0);
        	   request.setAttribute("classes", classes);
        	   request.setAttribute("building", building);
        	   return mapping.findForward("input2");
           }
           if(page==3)
           {
        	   BuildingService buildingService=(BuildingService)ContextUtil.getContext().getBean("buildingService");
       		DormitoryService dormitoryService=(DormitoryService)ContextUtil.getContext().getBean("dormitoryService");
       		Building building=null;
       		Long id=null;
       		try{
       			id=Long.valueOf(request.getParameter("building"));
       			building=buildingService.findById(id);
       		}
       		catch (Exception e) {
       			// TODO: handle exception
       			e.printStackTrace();
       		}
       		PageIterator<Dormitory> dormitorIter=dormitoryService.findByBuilding(0, 0, building);
       		request.setAttribute("dormitory", dormitorIter);
       		return mapping.findForward("input3");
           }
      }
    else {
        this.saveErrors(request, errors);
        if (page == 1){
            return mapping.findForward("input" + page);
        }

        if (page== 2){
            return mapping.findForward("input" + page);
        }
        if (page == 3){
            return mapping.findForward("input" + page);
        }
    }
		return mapping.findForward("input1");
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ActionErrors errors=new ActionErrors();
		
		StudentDTO studentInfo=(StudentDTO) form;
		String name=studentInfo.getStudentNumber();
//		System.out.println("成功");
		studentInfo.setName(name);
		StudentAffairTeacherWrapper studentAffairTeacherWrapper=(StudentAffairTeacherWrapper)ContextUtil.getContext().getBean("studentAffairTeacherWrapper");
		AuthenticationService authenticationService = (AuthenticationService) ContextUtil.getContext().getBean("authenticationService");
		boolean exist = authenticationService.isUserExist(name);
		if(exist)
		{
			errors.add("errors", new ActionMessage("该用户已存在", false));
			log.warn("用户已存在");
			this.addErrors(request, errors);
			return mapping.getInputForward();
		}
		try{
			studentAffairTeacherWrapper.addStudent(studentInfo);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			return mapping.findForward(Constant.FAILURE);
		}
		return mapping.findForward(Constant.SUCCESS);
}
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		long id=Long.valueOf(request.getParameter("id"));
		StudentAffairTeacherWrapper studentAffairTeacherWrapper=(StudentAffairTeacherWrapper)ContextUtil.getContext().getBean("studentAffairTeacherWrapper");
		try{
			studentAffairTeacherWrapper.deleteStudentById(id);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			return mapping.findForward(Constant.FAILURE);
			
		}
		return mapping.findForward(Constant.SUCCESS);
		
	}
	public ActionForward updateForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		Long studentId = null;
		StudentQueryService studentService = (StudentQueryService) ContextUtil.getContext().getBean("studentQueryService");
		try {
			studentId = Long.valueOf(request.getParameter("id"));
		} catch (Exception e) {
			log.error("Student Id 解析错误");
		}
		Student student = studentService.findById(studentId);
		SchoolService schoolService = (SchoolService) ContextUtil.getContext().getBean("schoolService");
		PageIterator<School> schoolIter = schoolService.findAllSchool(0, 0);
		request.setAttribute("student", student);
		request.setAttribute("school", schoolIter);
		return mapping.findForward("updateForm");
		
	}
	public ActionForward update(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		StudentDTO studentDto= (StudentDTO)form;
		StudentAffairTeacherWrapper studentAffairTeacherWrapper=(StudentAffairTeacherWrapper)ContextUtil.getContext().getBean("studentAffairTeacherWrapper");
		try{
			studentAffairTeacherWrapper.updateStudent(studentDto);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			return mapping.findForward(Constant.FAILURE);
		}
		return mapping.findForward(Constant.SUCCESS);
		
	}
	public ActionForward find(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response ){
		StudentQueryService studentQueryServicer= (StudentQueryService) ContextUtil.getContext().getBean("studentQueryService");
		PageIterator<Student> iter = studentQueryServicer.findAll(0, 100);
		
		request.setAttribute("students", iter);
		long id;
		try{
			id=Long.valueOf(request.getParameter("id"));
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			return mapping.findForward(Constant .FAILURE);
		}
		Student student=studentQueryServicer.findById(id);
		request.setAttribute("students", student);
		

		return mapping.findForward("update");
	}
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		return this.list(mapping, form, request, response);
	}
	

}
